<template>
  <div class="main-login-container">
    <div class="login-container">
      <div class="login">
        <img :src="loginBackgroundImgUrl" alt="" class="background-img">
        <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
          <div class="title">
            <span>欢迎登录</span>
          </div>
          <el-form-item prop="username">
            <el-input
              v-model="loginForm.username"
              type="text"
              auto-complete="off"
              placeholder="账号"
            >
              <svg-icon slot="prefix" icon-class="login-account" class="el-input__icon input-icon" />
            </el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              v-model="loginForm.password"
              type="password"
              auto-complete="off"
              placeholder="密码"
              @keyup.enter.native="handleLogin"
            >
              <svg-icon slot="prefix" icon-class="login-password" class="el-input__icon input-icon" />
            </el-input>
          </el-form-item>
          <el-form-item v-if="captchaOnOff" prop="code">
            <el-input
              v-model="loginForm.code"
              auto-complete="off"
              placeholder="验证码"
              style="width: calc(100% - 110px)"
              @keyup.enter.native="handleLogin"
            >
              <svg-icon slot="prefix" icon-class="login-verification-code" class="el-input__icon input-icon" />
            </el-input>
            <div class="login-code">
              <img :src="codeUrl" class="login-code-img" alt="验证码" @click="getCode">
            </div>
          </el-form-item>
          <el-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 25px 0px;">记住密码</el-checkbox>
          <el-form-item style="width:100%;">
            <el-button
              :loading="loading"
              size="medium"
              type="primary"
              style="width:100%;"
              @click.native.prevent="handleLogin"
            >
              <span v-if="!loading">登 录</span>
              <span v-else>登 录 中...</span>
            </el-button>
          </el-form-item>
        </el-form>
        <!--  底部  -->
        <div class="el-login-footer">
        </div>
      </div>
    </div>
    <div class="foot-container" />
  </div>
</template>

<script>
import { getCodeImg } from '@/api/login'
import Cookies from 'js-cookie'
import { encrypt, decrypt } from '@/utils/jsencrypt'
import defaultBackground from '@/assets/images/login-background.png'
export default {
  name: 'Login',
  data() {
    return {
      codeUrl: '',
      loginForm: {
        username: null,
        password: null,
        rememberMe: false,
        code: '',
        uuid: ''
      },
      loginRules: {
        username: [
          { required: true, trigger: 'blur', message: '请输入您的账号' }
        ],
        password: [
          { required: true, trigger: 'blur', message: '请输入您的密码' }
        ],
        code: [{ required: true, trigger: 'change', message: '请输入验证码' }]
      },
      loading: false,
      // 验证码开关
      captchaOnOff: false,
      // 注册开关
      register: false,
      redirect: undefined,
      encodePwdRule: 'none'
    }
  },
  computed: {
    loginBackgroundImgUrl() {
      return defaultBackground
    }
  },
  created() {
    this.getCode()
    this.getCookie()
    this.redirect = this.$route.query && this.$route.query.redirect
  },
  methods: {
    getCode() {
      getCodeImg().then(res => {
        const extData = res.extData || {}
        this.encodePwdRule = extData.encodePwdRule || 'none'
        this.captchaOnOff = extData.captchaOnOff
        if (this.captchaOnOff) {
          this.codeUrl = 'data:image/gif;base64,' + (extData.img || res.img)
          this.loginForm.uuid = (extData.uuid || res.uuid)
        }
      })
    },
    getCookie() {
      const username = Cookies.get('username')
      const password = Cookies.get('password')
      const rememberMe = Cookies.get('rememberMe')
      this.loginForm = {
        username: (username === null || username === undefined) ? this.loginForm.username : username,
        password: (password === null || password === undefined) ? this.loginForm.password : decrypt(password),
        rememberMe: (rememberMe === null || rememberMe === undefined) ? false : Boolean(rememberMe)
      }
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          if (this.loginForm.rememberMe) {
            // 点击记住密码的时候存到cookies中
            Cookies.set('username', this.loginForm.username, { expires: 30 })
            Cookies.set('password', encrypt(this.loginForm.password), { expires: 30 })
            Cookies.set('rememberMe', this.loginForm.rememberMe, { expires: 30 })
          } else {
            Cookies.remove('username')
            Cookies.remove('password')
            Cookies.remove('rememberMe')
          }
          const loginInfo = JSON.parse(JSON.stringify(this.loginForm))
          if (this.encodePwdRule === 'rsa') {
            loginInfo.password = encrypt(loginInfo.password)
          }
          this.$store.dispatch('Login', loginInfo).then(() => {
            this.$router.replace({ path: this.redirect || '/index' }).catch(() => {})
          }).catch((e) => {
            this.loading = false
            if (this.captchaOnOff) {
              this.getCode()
            }
            console.error(e)
          })
        }
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss">
  .main-login-container {
    width: 100%;
    height: 100%;
    overflow: hidden;
    .login-container {
      height: 100%;
      width: 100%;
      position: relative;
      .login {
        height: 100%;
        width: 100%;
        .background-img {
          width: 100%;
          height: 100%;
          object-fit: cover;
        }
      }
      .title {
        height: 50px;
        font-size: 20px;
        display: flex;
        justify-content: center;
        align-items: center;
        img {
          width: 206px;
          height: 25px;
        }
      }
      .login-form {
        box-shadow: 0 1px 6px rgba(0, 0, 0, 0.2);
        border-radius: 6px;
        background: #ffffff;
        width: 20%;
        max-width: 350px;
        min-width: 300px;
        padding: 25px 25px 5px 25px;
        position: fixed;
        right: 15%;
        top: calc(47% - 140px);
        z-index: 10;
        .el-input {
          height: 38px;
          input {
            height: 38px;
          }
        }
        .input-icon {
          height: 39px;
          width: 14px;
          margin-left: 2px;
        }
      }
      .login-tip {
        font-size: 13px;
        text-align: center;
        color: #bfbfbf;
      }
      .login-code {
        height: 38px;
        float: right;
        img {
          cursor: pointer;
          vertical-align: middle;
        }
      }
      .el-login-footer {
        height: 40px;
        line-height: 40px;
        position: absolute;
        bottom: 0;
        width: 100%;
        text-align: center;
        color: #fff;
        font-family: Arial;
        font-size: 12px;
        letter-spacing: 1px;
        opacity: 0.5;
      }
      .login-code-img {
        height: 38px;
      }
    }
  .foot-container {
    height: 15%;
  }
  }
.login-form .el-form-item--medium .el-form-item__content{
  margin-left: 0px !important;
}
</style>
